package com.project.dao.dao.impl;

import java.sql.*;

/**
 * 所有dao的父类
 */
public class BaseDao {
    /**连接对象*/
    protected Connection con;
    /**sql语句执行对象*/
    protected PreparedStatement ps;
    /**结果集对象*/
    protected ResultSet rs;

    /*
     * 加载驱动
     */
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 建立连接
     */
    public void setConnection(){
        try {
            this.con = DriverManager.getConnection("jdbc:mysql://localhost:12345/mydb?characterEncoding=utf-8",
                    "root","lovo");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public void closeConnection(){
        try {
            if (rs!=null) {
                rs.close();
            }
            if (ps!=null) {
                ps.close();
            }
            if (con!=null) {
                con.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
